<?php
class Fetch_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

    public function get_index($page) {
        $content = $this->common->fetch("http://tieba.baidu.com/f?kw=%D5%E3%BD%AD%B4%F3%D1%A7&pn={$page}", TRUE);

        $html = new simple_html_dom();
        $html->load($content);

        foreach ($html->find('.j_thread_list') as $key => $value) {
            echo '.' . ' ';
            $data_field = $value->getAttribute('data-field');

            if ($data_field == FALSE)
                continue;
            //部分的data_field 是被动态的注入的

            $c = $value->find('.threadlist_text a', 0);
            
            $data_field = htmlspecialchars_decode($data_field);

            $data = json_decode($data_field);

            $id = $data->id;
            $author_name = $data->author_name;
            $reply_num = $data->reply_num;
            $is_good = $data->is_good;
            $title = $c->getAttribute('title');
            // $title = iconv('GBK', 'UTF-8', $c->getAttribute('title'));

            $last_reply = $value->find('.tb_icon_author_rely a', 0);
            if ($last_reply != NULL) {
                $user_data_field = $last_reply->getAttribute('data-field');
                $last_reply = json_decode(htmlspecialchars_decode($user_data_field))->un;
            }

            if ($this->db->where('id', $id)->get('thread')->num_rows() != 0) {
                //已经保存了这一条数据
                $this->common->log('info', "Thread {$id} has already inserted, title: {$title}.");
                $this->db->update('thread', array(
                        'reply_num' => $reply_num,
                        'is_good'   => $is_good,
                        'data_field'    => $data_field,
                        'last_reply_user'   => $this->user->get_uid_by_name($last_reply)
                    ), array(
                        'id'    => $id
                    ));
            } else {
                $this->common->log('info', "Thread {$id} inserted, title: {$title}.");
                $this->db->insert('thread', array(
                        'id'                => $id,
                        'author_id'         => $this->user->get_uid_by_name($author_name),
                        'reply_num'         => $reply_num,
                        'is_good'           => $is_good,
                        'title'             => $title,
                        'data_field'        => $data_field,
                        'last_reply_user'   => $this->user->get_uid_by_name($last_reply)
                    ));
            }
        }
    }

    public function get_thread($id) {
        if ($id == FALSE)
            return FALSE;

        $content = $this->common->fetch('http://tieba.baidu.com/p/' . $id . '?pn=1', TRUE);

        $html = new simple_html_dom();
        $html->load($content);

        $total_page = intval($html->find('.l_reply_num span.red', 0)->plaintext);
        //总共有多少页

        if ($total_page < 1)
            $total_page = 1;

        for($i = 1; $i <= $total_page; ++$i){
            echo '.';
            $html->clear();
            unset($html);

            $content = $this->common->fetch("http://tieba.baidu.com/p/{$id}?pn={$i}", TRUE);
            $html = new simple_html_dom();
            $html->load($content);
            //加载下一页

            if (!count($html->find('.l_post'))) {
                return FALSE;
            }

            foreach ($html->find('.l_post') as $key => $value) {
                $data_field = $value->getAttribute('data-field');
                $data_field = htmlspecialchars_decode($data_field);
                $data = json_decode($data_field);

                $tid = $id;
                $pid = $data->content->id;
                $date = $data->content->date;
                $floor = $data->content->floor;
                $author_id = $data->author->id;
                $author_name = $data->author->name;
                $content = $value->find('.d_post_content', 0)->innertext;

                $lzl_data = $value->find('.lzl_li_pager', 0)->getAttribute('data-field');
                $lzl_data = htmlspecialchars_decode($lzl_data);
                $lzl_data = json_decode($lzl_data);

                $lzl_num = $lzl_data->total_num;
                $lzl_page = $lzl_data->total_page;

                if ($this->db->where('pid', $pid)->get('thread_reply')->num_rows() != 0) {
                    //已经保存了这一层
                    $this->db->update('thread_reply', array(
                            'lzl_num'       => $lzl_num,
                            'lzl_page'      => $lzl_page,
                            'data_field'    => $data_field
                        ), array(
                            'pid'   => $pid
                        ));
                } else {
                    $this->db->insert('thread_reply', array(
                            'tid'           => $tid,
                            'pid'           => $pid,
                            'data_field'    => $data_field,
                            'date'          => $date,
                            'floor'         => $floor,
                            'author_id'     => $author_id,
                            'content'       => $content,
                            'lzl_num'       => $lzl_num,
                            'lzl_page'      => $lzl_page,
                        ));
                }

                if ($this->db->affected_rows() != 0)
                    $this->user->notify_reply_update($author_id, $date);
                    //更新用户的最后回复时间
            }

            
        }
    }

    public function get_lzl($pid, $tid) {
        $content = $this->common->fetch("http://tieba.baidu.com/p/comment?tid={$tid}&pid={$pid}&pn=1", TRUE);

        $html = new simple_html_dom();
        $html->load($content);

        $total_page_text = $html->find('p.j_pager a', -1);
        $total_page = 1;    //假设只有一页

        if ($total_page_text != FALSE) {
            //有很多页
            $total_page_text = $total_page_text->getAttribute('href');
            preg_match('/[0-9]*/', $total_page_text, $match);
            $total_page = intval($match[0]);    
        }
        //总共有多少页

        for ($i = 1; $i <= $total_page; ++$i) {
            $html->clear();
            unset($html);

            $content = $this->common->fetch("http://tieba.baidu.com/p/comment?tid={$tid}&pid={$pid}&pn={$i}", TRUE);
            $html = new simple_html_dom();
            $html->load($content);
            //加载下一页

            foreach($html->find('li.lzl_single_post') AS $item) {
                $data_field = htmlspecialchars_decode($item->getAttribute('data-field'));
                $data = json_decode($data_field);

                $spid = $data->spid;
                $author_id  = $this->user->get_uid_by_name($data->user_name);
                $content    = $item->find('span.lzl_content_main', 0)->plaintext;
                $date       = $item->find('span.lzl_time', 0)->plaintext;

                if ($this->db->where('spid', $spid)->get('lzl')->num_rows() == 0) {
                    //这一条没有插入
                    $this->db->insert('lzl', array(
                            'spid'  => $spid,
                            'author_id' => $author_id,
                            'content'   => $content,
                            'tid'       => $tid,
                            'pid'       => $pid,
                            'date'      => trim($date)
                        ));
                    $this->user->notify_reply_update($author_id, $date);
                    //更新用户的最后回复时间
                } else {
                    //已经保存过了，更新一下时间防止重复抓取
                    $this->db->query('UPDATE lzl SET last_fetch=NOW() WHERE spid=?', array($spid));
                }

            }
        }
    }

    public function get_need_fetch_threads(){
        $sql = 'SELECT t.id 
                FROM thread AS t 
                WHERE (SELECT r.tid
                        FROM thread_reply AS r
                        WHERE r.tid=t.id 
                        LIMIT 1) IS NULL
                    OR t.last_fetch >= (SELECT max(r.last_fetch)
                                        FROM thread_reply AS r
                                        WHERE r.tid=t.id
                                        GROUP BY r.tid)
                LIMIT 1';
        //每次取一个，否则内存超限了
        $data = $this->db->query($sql);
        if ($data->num_rows() == 0)
            return NULL;

        return $data->row()->id;
    }

    public function get_need_fetch_lzl() {
        $sql = 'SELECT r.tid, r.pid 
                FROM thread_reply AS r
                WHERE ((SELECT l.spid 
                        FROM lzl AS l
                        WHERE l.tid=r.tid AND l.pid=r.pid
                        LIMIT 1) IS NULL
                    OR r.last_fetch >= (SELECT MAX(l.last_fetch)
                                        FROM lzl AS l
                                        WHERE l.tid=r.tid AND l.pid=r.pid
                                        GROUP BY l.pid))
                    AND r.lzl_num != 0
                LIMIT 10;';
        $data = $this->db->query($sql);
        if ($data->num_rows() == 0)
            return NULL;

        $data = $data->result();

        $id = array();
        foreach($data AS &$value) {
            $id[] = array('tid' => $value->tid, 'pid' => $value->pid);
        }
        return $id;
    }

}
?>